import "nprogress/nprogress.css";
import {createRouter, createWebHistory} from "vue-router";
import {ElMessage} from "element-plus";

let routes = [
    {
        path: "/login",
        name: "login",
        component: ()=>import('@/views/login/index.vue'),
    },
    {
        path: "/",
        component: ()=>import('@/layout/index.vue'),
        name: "container",
        redirect: "home",
        meta: {
            requiresAuth: true, //有一些页面是否登录才能进去
            name: "首页",
        },
        children: [{
            path: "/home",
            name: "home",
            component: ()=>import("@/views/home/index.vue"),
            meta: {
                requiresAuth: true, //有一些页面是否登录才能进去
                name: "首页",
            }
        }]
    },
    {
        path: '/404',
        name:'404',
        component:()=>import('@/views/ErrorMessage/404.vue')
    },
   /* /!*找不到的地址都跳转404*!/
    {
        path: '/:pathPatch(.*)',
        redirect:'/404'
    }*/
]

let router = createRouter({
    history: createWebHistory(),
    routes,
});

//路由守卫
router.beforeEach((to, from, next) => {
    //拿到登录的token
    let token = sessionStorage.getItem("token");
    //对需要登录的路由地址进行验证登录
    if (to.meta.requiresAuth && token == null) {
        ElMessage.error('请先登录!!')
        next({ path:'/login' })
    }else {
        next()
    }
})

export default router;
